package com.awesome.common.base.mvvm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * Author: JFangZ
 * Email:  janezhang1901@163.com
 * Date:2021/5/19 9:38
 * Description:The base of view model.If want to use MVVM frame,please inherit this.
 */
abstract class BaseViewModel<REPO : BaseRepository?>() : ViewModel() {
    protected val repository: REPO by lazy { (ViewModelUtils.getClass<REPO>(this)).newInstance() }

    protected fun execute(block: () -> Unit) {
        viewModelScope.launch(Dispatchers.IO) {
            block()
        }
    }

    protected fun executeSuspend(block: suspend () -> Unit) {
        viewModelScope.launch {
            block()
        }
    }
}
